﻿//***********************************************************************
// Assembly         : NMortgage.UI
// Author           : Mike
// Created          : 04-29-2012
//
// Last Modified By : Mike
// Last Modified On : 04-29-2012
// Description      : 
//
// Copyright        : (c) . All rights reserved.
//***********************************************************************
namespace NMortgage.UI.Model
{
    using System.Collections.ObjectModel;

    public abstract class AEntityBaseCollection<TEntity> : ObservableCollection<TEntity>
        where TEntity : class
    {
        #region Methods

        public new void Add(TEntity entity)
        {
            int index = IndexOf(entity);

            if (index >= 0)
            {
                base.RemoveItem(index);
                base.InsertItem(index, entity);
            }
            else
            {
                base.Add(entity);
            }
        }

        public bool CanMoveDown(TEntity entity)
        {
            int index = this.IndexOf(entity);
            return index != this.Count - 1;
        }

        public bool CanMoveUp(TEntity entity)
        {
            int index = this.IndexOf(entity);
            return index != 0;
        }

        public void MoveDown(TEntity entity)
        {
            if (!CanMoveDown(entity)) return;
            int oldIndex = this.IndexOf(entity);
            int newIndex = oldIndex + 1;
            this.MoveItem(oldIndex, newIndex);
        }

        public void MoveUp(TEntity entity)
        {
            if (!CanMoveUp(entity)) return;
            int oldIndex = this.IndexOf(entity);
            int newIndex = oldIndex - 1;
            this.MoveItem(oldIndex, newIndex);
        }

        #endregion Methods
    }
}